(function () {
    'use strict';

    angular.module('KingAdmin.pages.pdev.devctrl')
        .controller('DevctrlListCtrl', DevctrlListCtrl);

    /** @ngInject */
    function DevctrlListCtrl($scope, $filter, $interval, DevctrlService, DeviceService, DictService) {

        var kt = this;
        kt.devctrllist = [];
        kt.status = [];
        kt.timer = null;

        kt.LoadPage = function (tableState) {
            tableState = tableState || kt.tableState;
            tableState.pagination.number = tableState.pagination.number || 10;
            DevctrlService.getSmartData(tableState,
                function (data) {
                    tableState.pagination.numberOfPages = data.result.pages;
                    tableState.pagination.totalItemCount = data.result.total;
                    kt.tableState = tableState;
                    kt.devctrllist = data.result.records;

                    if (kt.timer == null) {
                        kt.timer = $interval(kt.updCtrlList, 5000);
                    }
                });
        };

        kt.getDictLabel = DictService.getDictLabel;
        kt.showDevName = DeviceService.showDevName;

        //删除
        kt.del = function (id) {
            DevctrlService.del({id: id},
                function (data) {
                    kt.LoadPage();
                })
        };
        kt.checkboxes = {
            checked: false,
            items: {}
        };

        kt.updCtrlList = function () {
            angular.forEach(kt.devctrllist, function (ctrl, index, array) {
                console.log("upd Ctrl list");
                console.log(ctrl);
                DevctrlService.getInfo({id:ctrl.id},
                    function (data) {
                    ctrl.isOnline = data.isOnline;
                    ctrl.sw1Status = data.sw1Status;
                })
            })
        };

        kt.openSw = function(devctrl) {
            console.log("打开设备");
            var title = '打开设备';
            var optMsg = DevctrlService.buildWriteOpt(devctrl.deviceUid, devctrl.sw1InsOpen);

            console.log(optMsg);
            console.log(devctrl);
            var param = {
                deviceUid: devctrl.deviceUid,
                optMsg: optMsg,
                id: devctrl.id
            };

            kt.ctrlDev(param, title);
        };

        kt.closeSw = function (devctrl) {
            var title = '关闭设备';
            console.log("关闭设备");
            var optMsg = DevctrlService.buildWriteOpt(devctrl.deviceUid, devctrl.sw1InsClose);
            var param = {
                deviceUid: devctrl.deviceUid,
                optMsg: optMsg,
                id: devctrl.id
            };

            kt.ctrlDev(param, title);
        };

        kt.reboot = function (devctrl) {
            console.log("关闭设备");
            var title = '重启设备';
            var optMsg = DevctrlService.buildWriteMsg(devctrl.deviceUid, devctrl.rebootIns);
            var param = {
                deviceUid: devctrl.deviceUid,
                optMsg: optMsg,
                id: devctrl.id
            };

            kt.ctrlDev(param, title);
        };

        kt.shutdown = function (devctrl) {
            console.log("关闭设备");
            var title = '设备停机';
            var optMsg = DevctrlService.buildWriteMsg(devctrl.deviceUid, devctrl.shutdownIns);
            var param = {
                deviceUid: devctrl.deviceUid,
                optMsg: optMsg,
                id: devctrl.id
            };

            kt.ctrlDev(param, title);
        };

        kt.ctrlDev = function (param, title) {
            DevctrlService.ctrl(param, title, function (data) {
                // refresh page
                console.log(data);
            })
        };

        // 更新设备信息
        kt.refresh = function (devctrl) {
            console.log("关闭设备");
            var title = '设备停机';
            var swReadMsg = '0B02' + intToHex(devctrl.sw1ChnNum, 2) + '00';
            var optMsg = DevctrlService.buildReadOpt(devctrl.deviceUid, swReadMsg);
            var param = {
                deviceUid: devctrl.deviceUid,
                optMsg: optMsg,
                id: devctrl.id
            };

            DevctrlService.ctrlRead(param, title, function (data) {
            });
        };

        function intToHex(val, len) {
            var hexStr = val.toString(16);
            var zeroStr = '';

            for (var i=0; i < len - hexStr.length; i++) {
                zeroStr += '0';
            }
            return zeroStr + hexStr;
        }

        // $scope.$watch('kt.checkboxes.checked', function (value) {
        //     angular.forEach(kt.userlist, function (item) {
        //         kt.checkboxes.items[item.id] = value;
        //     });
        // });

        // 离开页面清除timer
        $scope.$on("$destroy", function() {
            if (kt.timer) {
                $interval.cancel(kt.timer);
            }
        })
    }

})();
